// pages/account/accountinfo.js
var app = getApp();
var util = require('../../utils/util.js');
import WxValidate from '../../utils/validate';

var inputContent = {};//输入内容
Page({

    /**
     * 页面的初始数据
     */
    data: {
        btn: false,
        vcdisabled: true,
        items: [
            {id: '1', value: '我同意《贷投狗用户服务协议及隐私声明》', checked: 'true'},
          ],
          code:"",
          phoneNumber:"",
          scene: null,
          name:"",
          paracont: "获取验证码",//验证码文字
          addtell: {
            addtellHidden: true, //弹出框显示/隐藏
        },
    },
    footAddtell: function () {
        //打开弹出框
        this.setData({
            addtell: {
                addtellHidden: false,
                contract_info: ''
            }
        })
    },
    modalConfirm: function () {
        //弹出框确认操作
        this.setData({
            addtell: {
                addtellHidden: true,
            }
        })
    },
    modalCancel: function () {
        //弹出框取消操作
        this.setData({
            addtell: {
                addtellHidden: true,
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const scene = decodeURIComponent(options.scene)
        this.setData({
            scene:scene
        })
        console.log(scene,"----")
        this.getList(scene);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    subim:function(e) {
        let that = this
        let checkbox = that.data.items[0].checked
        if (!checkbox) {
            wx.showToast({
              title: '请勾选"我同意"',
              icon: 'none',
            })
            return false
        }
            if(!/^1[3456789]\d{9}$/.test(that.data.phoneNumber) ) {       
                wx.showToast({
                    title: '请输入手机号',
                    icon: 'none',
                  })
                  return false
                 } 
                 if(!that.data.code) {
                    wx.showToast({
                        title: '请输入验证码',
                        icon: 'none',
                      })
                      return false
            }
            let data = {}
            if(that.data.scene == "undefined") {
               data.phoneNumber= that.data.phoneNumber
               data.code=that.data.code
            }else {
                data.phoneNumber= that.data.phoneNumber
                data.code=that.data.code
                data.inviteId=that.data.scene
            }
            console.log(data)
                wx.request({
                    url: app.globalData.api + '/app/auto/registerAgent', //上线的话必须是https，没有appId的本地请求貌似不受影响 
                    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT 
                    header: {
                        'Content-Type': "application/json",
                        'Cookie': 'SESSION=' + wx.getStorageSync("sessionId")
                    }, // 设置请求的 header
                    data,
                    success: function (res) {
                        console.log(res.data)
                        if (res.data.code == 200) {
                            wx.showModal({
                                title: '提示',
                                content: res.data.message,
                                showCancel: false,
                                confirmText: '确定',
                                success: function (res) {
                                    if (res.confirm) {
                                        wx.redirectTo({
                                            url: '/pages/startPage/startPage'
                                        })
                                    }
                                }
                            })
                            wx.setStorageSync('registerId', res.data.data)
                        }else if(res.data.code == 222) {
                            wx.showModal({
                                title: '提示',
                                content: res.data.message,
                                showCancel: false,
                                confirmText: '去登录',
                                success: function (res) {
                                    if (res.confirm) {
                                        wx.redirectTo({
                                            url: '/pages/account/login'
                                        })
                                    }
                                }
                            })
                        }else {
                            wx.showModal({
                                title: '警告',
                                content: res.data.message,
                                showCancel: false,
                                confirmText: '确定',
                            })
                        }
                    },
                    fail: function () {
                    },
                    complete: function () {
                        // complete 
                    }
                })


    },
    bindChange: function (e) {
        console.log(e)
        inputContent[e.currentTarget.id] = e.detail.value;
        util.verifyCodeBtn(e, this);
        this.setData({
            phoneNumber: e.detail.value
        })
    },
    bindCode:function (e) {
        console.log(e)
        this.setData({
            code: e.detail.value
        })
    },
    getVerifyCode: function (e) {
        var that = this;
        util.getVerifyCode(inputContent['user'], this, function (data) {
            that.setData({
                verifycode: data.data
            })
        })
    },
    checkboxChange(e) {
        console.log(e)
        let checkbox = e.detail.value
        if (checkbox.length<=0){
            this.data.items[0].checked = false
        } else {
            this.data.items[0].checked = true
        }
      },
    //   获取扫码数据
    getList:function (obj) {
        var that = this
        if(obj !== "undefined") {
            console.log(obj,'0')
            util.https(app.globalData.api + "/app/auto/empName/" + obj, "GET", {},
            function (data) {
                if (data.code == 200) {
                    that.setData({
                        name: data.data
                    })
                } else {
                    util.toolTip(that, data.message)
                }
            }
        )
        }    
        }
})